package com.cloudcc.mobile.parser;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;

import com.cloudcc.mobile.entity.LeadEntity;
import com.cloudcc.mobile.entity.LeadMessage;
import com.cloudcc.mobile.util.Json;
import com.cloudcc.mobile.util.Tools;

public class LeadParser {
	/**
	 * 解析查询的客户
	 * @param jsonString
	 * @return
	 */
	public  static LeadMessage getLead(String jsonString){
		ArrayList<LeadEntity> list=new ArrayList<LeadEntity>();
		LeadMessage message=new LeadMessage();
		try {
			JSONObject obj=new JSONObject(jsonString);
			message.setResult(obj.getBoolean("result"));
			message.setReturnInfo(obj.getString("returnInfo"));
			message.setReturnCode(obj.getString("returnCode"));
			JSONArray json=obj.getJSONArray("data");
			for (int i = 0; i < json.length(); i++) {
				JSONObject jsonObj=json.getJSONObject(i);
				LeadEntity lead=(LeadEntity) Json.toObject(jsonObj.toString(),LeadEntity.class);
				list.add(lead);
			}
			message.setList(list);
		} catch (JSONException e) {
			Tools.handle(e);
		}
		return message;
	}
	
	/**
	 * 获取所有客户
	 * 
	 */
	
	public static Map<String,LeadEntity> getAllCallRecords(Context context,String jsonString) {
		LeadMessage leadMessage=getLead(jsonString);
		if(!leadMessage.isResult()){
			return null;
		}
		List<LeadEntity> lead=leadMessage.getList();
		Map<String,LeadEntity> map=new HashMap<String, LeadEntity>();
		if(lead!=null){
			for (int i = 0; i < lead.size(); i++) {
				String name=lead.get(i).getName();
				map.put(name, lead.get(i));
			}
			return map;
		}
		return null;
	}
	
	
	
	
}
